package game.action;

import game.db.DBUtil;
import game.socket.GameThread;
import game.utils.DateUtils;
import game.utils.PropertiesUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;

public abstract class BaseAction {
	protected static Logger log = Logger.getLogger(BaseAction.class);
	private static Map<String, BaseAction> actionMap = new HashMap();//action映射
	private DBUtil dbUtil;
	protected Map params;//参数集合
	
	public static void main(String[] args) {
		Map map = PropertiesUtils.getMapByAbsPath("action_mapping.properties");
		Iterator iter = map.keySet().iterator();
		while(iter.hasNext()){
			String key = iter.next().toString();
			System.out.println("actionMap.put(\""+key+"\", new "+map.get(key)+"());");
		}
	}
	
	public Map getParams() {
		return params;
	}

	static{//初始化action映射
		Map map = PropertiesUtils.getMapByAbsPath("action_mapping.properties");
		Iterator iter = map.keySet().iterator();
		while(iter.hasNext()){
			String key = iter.next().toString();
			String className = String.valueOf(map.get(key));
			try {
				actionMap.put(key, (BaseAction)Class.forName(className).newInstance());
			} catch (Exception e) {
				log.error("异常",e);
			} 
		}
	}
	private boolean isLogin;//是否登录
	private boolean isValidataLogin = true;//是否校验登录
	private GameThread gameThread;
	private String head;
	
	public boolean isLogin() {
		return isLogin;
	}

	public String getHead() {
		return head;
	}

	public void setHead(String head) {
		this.head = head;
	}

	public void setLogin(boolean isLogin) {
		this.isLogin = isLogin;
	}

	public boolean isValidataLogin() {
		return isValidataLogin;
	}

	public void setValidataLogin(boolean isValidataLogin) {
		this.isValidataLogin = isValidataLogin;
	}

	public DBUtil getDbUtil() {
		if(this.dbUtil==null){
			this.dbUtil = new DBUtil();
		}
		return this.dbUtil;
	}

	public void setDbUtil(DBUtil dbUtil) {
		this.dbUtil = dbUtil;
	}
	
	public DBUtil setDbUtil() {
		if(this.dbUtil==null){
			this.dbUtil = new DBUtil();
		}
		return this.dbUtil;
	}

	/**
	 * <p>转换参数</p>
	 * @param msg 传递过来的数据
	 * @return
	 */
	public abstract String initParams();
	
	public void setParams(Map params) {
		this.params = params;
	}

	/**
	 * <p>校验是否可以执行</p>
	 * @return
	 */
	public abstract boolean validate();
	
	/**
	 * <p>执行</p>
	 * @return
	 */
	public abstract void execute();
	
	
	public GameThread getGameThread() {
		return gameThread;
	}

	public void setGameThread(GameThread gameThread) {
		this.gameThread = gameThread;
	}

	public synchronized static BaseAction getAction(String actionName){
		try {
			BaseAction ba = actionMap.get(actionName);
			return ba==null?null:ba.getClass().newInstance();
		} catch (InstantiationException e) {
			log.error("异常："+e);
			return null;
		} catch (IllegalAccessException e) {
			log.error("异常："+e);
			return null;
		}
	}
}
